package com.dzk.cp.util;

import java.util.Map;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.lang.StringUtils;

public class HttpUtils {
	
	public static String request(String url, Map<String, String> params, String charset) {
		String result = null;
		PostMethod postMethod = null;
		try {
			postMethod = new PostMethod(url);
			if (StringUtils.isEmpty(charset)) {
				charset = "UTF-8";
			}
			postMethod.setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, charset);
			if (params != null && params.size() > 0) {
				for (String name : params.keySet()) {
					postMethod.setParameter(name, params.get(name));
				}
			}
			HttpClient httpClient = new HttpClient();
			httpClient.executeMethod(postMethod);
			result = new String(postMethod.getResponseBody(), charset).trim();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (postMethod != null) {
				postMethod.releaseConnection();
			}
		}
		return result;
	}
	
	public static String requestUTF8(String url, Map<String, String> params) {
		return request(url, params, "UTF-8");
	}
	
	public static String requestGBK(String url, Map<String, String> params) {
		return request(url, params, "GBK");
	}

}
